package group2assignone.ucm.customer;

import group2assignone.func.Authentication;
import group2assignone.dao.ClientComms;
import group2assignone.model.Customer;
import group2assignone.ucm.UCController;
import group2assignone.ucm.UCStartup;
import group2assignone.view.MainView;
import group2assignone.view.UITool;
import group2assignone.view.customer.CustomerWelcomeView;

/**
 *Use Case Controller to Log in a Customer.
 * 
 */
public class UCCustomerLogin implements UCController {

    private Customer customer;
    private MainView view;
    private Authentication auth;
    private boolean ok,  ok2;
    private ClientComms client;

    /**
     * sets the view 
     * @param view
     */
    public UCCustomerLogin(MainView view) {
        this.view = view;
    }

    /**
     * Use Case: Log in Customer
     * Pre-Condition: MainView UI 
     * Post-Condition: UI is updated 
     * 1. Customer inputs login details and presses Login  
     * 2. System gets login details from view
     * 3. System authenticates login details
     * 4. System logs Customer in and updates UI
     * 3a.  - Customer provides incorrect login details
     *      - System displays error message and prompts user to reenter or cancel
     * 3b.  - System cannot log user in (connection error)
     *      - System displays error message and prompts user to try again later
     * 
     */
    public void run() {
        setup();
        view.upload(customer);
        ok = auth.authCustomer(customer);
        if (ok) {
            ok2 = client.login(customer);
            if (ok2) {
                CustomerWelcomeView gui = new CustomerWelcomeView(customer);
                gui.go();
                view.dispose();
            } else {
                UITool.displayError("Cannot log you in", view);
            }
        } else {
            UITool.displayError(auth.getErrorMsg(), view);
        }
    }

    public void setup() {
        customer = new Customer();
        auth = new Authentication();
        client = UCStartup.getInstance().getClientComms();
    }
}
    

